package com.pikaqiu.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pikaqiu.warehouse.config.BaseContext;
import com.pikaqiu.warehouse.domain.common.ReqCommonParams;
import com.pikaqiu.warehouse.domain.dto.CategoryDto;
import com.pikaqiu.warehouse.domain.entity.Category;
import com.pikaqiu.warehouse.domain.enums.CategoryTypeEnum;
import com.pikaqiu.warehouse.domain.req.CategoryForm;
import com.pikaqiu.warehouse.exception.MiniServiceException;
import com.pikaqiu.warehouse.mapper.CategoryMapper;
import com.pikaqiu.warehouse.service.CategoryService;
import com.pikaqiu.warehouse.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【category(类别表)】的数据库操作Service实现
* @createDate 2024-08-14 23:11:02
*/
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
    implements CategoryService{

    @Resource
    private UserService userService;

    @Override
    public List<CategoryDto> listByType(Integer type) {
        userService.checkUser();
        ReqCommonParams reqCommonParams = BaseContext.get();
        List<Category> list = list(new QueryWrapper<Category>().eq("company_id", reqCommonParams.getCompanyId())
            .eq("type", type).orderByDesc("sorted"));
        return list.stream().map(CategoryDto::new).collect(Collectors.toList());
    }

    @Override
    public void saveCategory(CategoryForm categoryForm) {
        userService.checkUser();
        ReqCommonParams reqCommonParams = BaseContext.get();
        list(new QueryWrapper<Category>().eq("company_id", reqCommonParams.getCompanyId())
            .eq("type", categoryForm.getType())
            .eq("name", categoryForm.getName())).stream().findFirst().ifPresent(category -> {
            throw new MiniServiceException("类别名称已存在");
        });
        Category category = new Category();
        category.setId(categoryForm.getId());
        category.setCompanyId(reqCommonParams.getCompanyId());
        category.setType(categoryForm.getType());
        category.setName(categoryForm.getName());
        save(category);
    }

    @Override
    public List<Category> listProductCategory(Integer companyId) {
        return list(new QueryWrapper<Category>().eq("company_id", companyId)
            .eq("type", CategoryTypeEnum.PRODUCT.getCode()).orderByDesc("sorted"));
    }

    @Override
    public List<Category> listCategory(Integer companyId) {
        return list(new QueryWrapper<Category>().eq("company_id", companyId).orderByDesc("sorted"));
    }

    @Override
    public List<Category> listMaterialsCategory(Integer companyId) {
        return list(new QueryWrapper<Category>().eq("company_id", companyId)
            .eq("type", CategoryTypeEnum.MATERIAL.getCode()).orderByDesc("sorted"));
    }
}




